package com.dmall.play.interview.chapter09;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class Leetcode343V0 {

    public int integerBreak(int n) {
        List<List<Integer>> combinations = breakInteger(n);
        int max = 0;
        for (List<Integer> combination : combinations) {
            int product = 1;
            for (Integer num : combination) {
                product *= num;
            }
            max = Math.max(max, product);
        }
        return max;
    }

    public List<List<Integer>> breakInteger(int n) {
        Deque<Integer> path = new ArrayDeque<>();
        List<List<Integer>> res = new ArrayList<>();
        backTrack(n, 1, path, res);
        return res;
    }

    public void backTrack(int n, int start, Deque<Integer> path, List<List<Integer>> res) {
        if (n == 0 && path.size() >= 2) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = start; i <= n; i++) {
            path.add(i);
            //第二个参数i，因为可以重复使用
            backTrack(n - i, i, path, res);
            path.removeLast();
        }
    }

    public static void main(String[] args) {
        /*Leetcode343V0 leetcode343 = new Leetcode343V0();
        System.out.println(leetcode343.breakInteger(5));*/

        Leetcode343V0 leetcode343 = new Leetcode343V0();
        System.out.println(leetcode343.integerBreak(70));
    }
}
